//
//  XGCalculator.swift
//  TodayNews
//
//  Created by monkey on 2020/3/26.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import YYText

struct XGCalculator
{
    /// 计算富文本高度
    /// - Parameters:
    ///   - text: 富文本
    ///   - width: 宽度
    ///   - maxHeight: 最大高度
    static func calculateAttributeString(text: NSAttributedString, width: CGFloat, maxHeight: CGFloat = CGFloat(MAXFLOAT)) -> CGFloat
    {
        let height = ceil(YYTextLayout(containerSize: CGSize(width: width, height: CGFloat(MAXFLOAT)), text: text)?.textBoundingSize.height ?? 0)
        return fmin(height, maxHeight)
    }
    
    /// 计算文本高度
    /// - Parameters:
    ///   - text: 文本
    ///   - width: 宽度
    ///   - fontSize: 字体大小
    ///   - maxHeight: 最大高度
    static func calculateString(text: String, width: CGFloat,fontSize: CGFloat, maxHeight: CGFloat = CGFloat(MAXFLOAT)) -> CGFloat
    {
         let height = (text as NSString).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: [.usesLineFragmentOrigin,.usesFontLeading], attributes: [.font: UIFont.systemFont(ofSize: fontSize)], context: nil).size.height
        return fmin(height, maxHeight)
    }
}
